package com.ict.txmsv2.controller.studentinfo;

import cn.hutool.core.util.StrUtil;
import com.ict.txmsv2.dto.StudentInfoQueryDto;
import com.ict.txmsv2.model.*;
import com.ict.txmsv2.services.CompetitionPrizeService;
import com.ict.txmsv2.services.ProjectEstablishService;
import com.ict.txmsv2.services.StudentInfoService;
import com.ict.txmsv2.services.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @Author lht
 * @date 2021/4/21 - 21:12
 * @description: 工作室学生业绩信息控制类
 */
@Api(tags = "学生竞赛等等信息的控制类")
@RestController
@RequestMapping("/studentInfo")
public class StudentInfoController {

    @Resource
    private CompetitionPrizeService competitionPrizeService;
    @Resource
    private ProjectEstablishService projectEstablishService;
    @Resource
    private UploadService uploadService;

    /**
     * 竞赛奖励部分
     * @param competitionPrize
     * @return
     */
    @PostMapping("/competitionPrize")
    @ApiOperation(value = "添加一个竞赛奖励到数据库")
    public RespBean inputCompetitionPrizeInfo(@RequestBody CompetitionPrize competitionPrize) {
        return competitionPrizeService.save(competitionPrize);
    }

    @GetMapping("/competitionPrize")
    @ApiOperation(value = "分页模糊查询竞赛奖励信息")
    public RespPageBean getCompetitionPrizeList(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer size,
                                                StudentInfoQueryDto queryDto) {
        return competitionPrizeService.list(page, size, queryDto);
    }

    @DeleteMapping("/competitionPrize/{id}")
    public RespBean deleteCompetitionPrizeById(@PathVariable Integer id) {
        return competitionPrizeService.deleteCompetitionPrizeById(id);
    }

    @PutMapping("/competitionPrize")
    public RespBean updateCompetitionPrize(@RequestBody CompetitionPrize competitionPrize) {
        return competitionPrizeService.updateCompetitionPrize(competitionPrize);
    }

    @ApiOperation(value = "通过用户名获取获奖信息")
    @GetMapping("/competitionPrizeEmp")
    public RespPageBean getCompetitionPrizeEmpByName(@RequestParam(defaultValue = "1") Integer page,
                                                     @RequestParam(defaultValue = "4") Integer size,
                                                     String name) {
        return competitionPrizeService.getCompetitionPrizeEmpByName(page, size, name);
    }

    /**
     * 课题立项部分
     */

    @PostMapping("/projectEstablish")
    public RespBean inputProjectEstablishInfo(@RequestBody ProjectEstablish projectEstablish) {
        return projectEstablishService.save(projectEstablish);
    }

    @GetMapping("/projectEstablish")
    public RespPageBean getProjectEstablishList(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer size,
                                                StudentInfoQueryDto queryDto) {
        return projectEstablishService.list(page, size, queryDto);
    }

    @DeleteMapping("/projectEstablish/{id}")
    public RespBean deleteProjectEstablishById(@PathVariable Integer id) {
        return projectEstablishService.deleteProjectEstablishById(id);
    }

    @PutMapping("/projectEstablish")
    public RespBean updateProjectEstablish(@RequestBody ProjectEstablish projectEstablish) {
        return projectEstablishService.updateProjectEstablish(projectEstablish);
    }

    @PostMapping("/uploadHonorary")
    public RespBean uploadHonorary(MultipartFile file) {
        String url = uploadService.uploadHonorary(file);
        if (!StrUtil.isEmpty(url)) {
            return RespBean.ok("文件上传成功!", url);
        }
        return RespBean.error("上传失败，请联系管理员!");
    }

}
